﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class UIManager : MonoBehaviour
{
	public static UIManager instance;

	public GameObject addTowerWindow;

	public Text txtGold;
	public Text txtWave;
	public Text txtEscapedEnemies;

	public GameObject towerInfoWindow;

	public GameObject winGameWindow;
	public GameObject loseGameWindow;
	public GameObject blackBackground;

	public Transform enemyHealthBars;
	public GameObject enemyHealthBarPrefab;

	public GameObject centerWindow;

	public GameObject damageCanvas;
	void Awake ()
	{
		instance = this;
	}

	private void UpdateTopBar ()
	{
		txtGold.text = GameManager.instance.gold.ToString ();
		txtWave.text = "Wave" + GameManager.instance.waveNumber + "/" + WaveManager.Instance.enemyWaves.Count;
		txtEscapedEnemies.text = "Escaped Enemies" + GameManager.instance.escapedEnemies + "/" + GameManager.instance.maxAllowedEscapedEnemies;
	}

	public void ShowAddTowerWindow (GameObject towerSlot)
	{
		addTowerWindow.SetActive (true);
		addTowerWindow.GetComponent<AddTowerWindow> ().towerSlotToAddTowerTo = towerSlot;

		UtilityMethods.MoveUiElementToWorldPosition (addTowerWindow.GetComponent<RectTransform> (), towerSlot.transform.position);
	}

	void Update(){
		UpdateTopBar();
	}

	public void ShowTowerInfoWindow(Tower tower){
		towerInfoWindow.GetComponent<TowerInfoWindow> ().tower = tower;
		towerInfoWindow.SetActive (true);
		UtilityMethods.MoveUiElementToWorldPosition (towerInfoWindow.GetComponent<RectTransform>(),tower.transform.position);
	}

	public void ShowWinScreen(){
		blackBackground.SetActive (true);
		winGameWindow.SetActive (true);
	}

	public void ShowLoseScreen(){
		blackBackground.SetActive (true);
		loseGameWindow.SetActive (true);
	}

	public void CreateHealthBarForEnemy(Enemy enemy){
		GameObject healthBar = Instantiate (enemyHealthBarPrefab);
		healthBar.transform.SetParent (enemyHealthBars,false);
		healthBar.GetComponent<EnemyHealthBar> ().enemy=enemy;
	}

	public void ShowCenterWindow(string text){
		centerWindow.transform.Find ("TxtWave").GetComponent<Text> ().text=text;
		StartCoroutine (EnableAndDisableCenterWindow());
	}
	private IEnumerator EnableAndDisableCenterWindow(){
		for(int i=0;i<3;i++){
			yield return new WaitForSeconds (0.4f);
			centerWindow.SetActive (true);
			yield return new WaitForSeconds (0.4f);
			centerWindow.SetActive (false);
		}
	}

	public void ShowDamage(){
		StartCoroutine (DoDamageAnimation());
	}

	private IEnumerator DoDamageAnimation(){
		for(int i=0;i<3;i++){
			yield return new WaitForSeconds (0.1f);
			damageCanvas.SetActive (true);
			yield return new WaitForSeconds (0.1f);
			damageCanvas.SetActive (false);
		}
	}
}
